Ext.define('Fxt.stl-sell.Controller', {
    extend: 'Fxt.core.CrudController',
    stores: 'Fxt.stl-sell.Store',
    models: ['Fxt.stl-sell.Model'],
    views: ['Fxt.stl-sell.view.Viewport', 'Fxt.stl-sell.view.Grid', 'Fxt.stl-sell.view.QueryForm'],
    $onInit: function() {
        var me = this;
        var queryForm = me.$getViewport().$getQueryForm();
        var grid = me.$getViewport().$getGrid();
        var module = me.$getModule();
        this.control({
            'splitbutton[action=new-sell]': {
                arrowclick: function(btn) {
                    btn.menu.removeAll();
                    Fxt.ajax({
                        maskCmp: btn,
                        method: 'GET',
                        url: '/lookup/stl-sell-alipay-account',
                        success: function(data) {
                            Ext.each(data, function(row) {
                                btn.menu.add({
                                    text: row.realName + ' - ' + row.jyAmountTotal,
                                    handler: function () {
                                        me.$newSell(row.id);
                                    }
                                });
                            });
                            btn.showMenu();
                        }
                    });
                }
            },
            'button[action=sync-sell-jy-status]': {
                click: function(btn) {
                    Fxt.ajax({
                        method: 'POST',
                        url: '/stl/sync-sell-jy-status',
                        success: function (data) {
                            me.$doQuery();
                        }
                    });
                }
            },
            'button[action=make-sell-jy]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        Fxt.ajax({
                            method: 'POST',
                            url: '/stl/make-sell-jy',
                            params: {
                                id: record.get('id')
                            },
                            success: function (data) {
                                me.$doQuery();
                            }
                        });
                    });
                }
            },
            'button[action=new-sell-auto-pay-account]': {
                click: function(btn) {
                    Fxt.ajax({
                        method: 'POST',
                        url: '/stl/new-sell-auto-pay-account',
                        success: function (data) {
                            me.$doQuery();
                        }
                    });
                }
            },
            'button[action=cancel-sell]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        Ext.Msg.confirm('提示', '确认要取消吗？', function(ok) {
                            if (ok != 'yes') {
                                return;
                            }
                            Fxt.ajax({
                                method: 'POST',
                                url: '/stl/cancel-sell',
                                params: {
                                    id: record.get('id')
                                },
                                success: function (data) {
                                    me.$doQuery();
                                }
                            });
                        });
                    });
                }
            },
            'splitbutton[action=force-set-sell-new-alipay]': {
                arrowclick: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        btn.menu.removeAll();
                        var id = record.get('id');
                        Fxt.ajax({
                            method: 'GET',
                            url: '/lookup/stl-sell-alipay-account',
                            success: function(data) {
                                Ext.each(data, function(row) {
                                    btn.menu.add({
                                        text: row.realName,
                                        handler: function () {
                                            me.$forceSetSellNewAlipay(id, row.id);
                                        }
                                    });
                                });
                                btn.showMenu();
                            }
                        });
                    });
                }
            },
            'grid': {
                'cellclick': function(grid, td, cellIndex, record) {
                    if (cellIndex == 10) {
                        var payImgUrl = '/tools/get?url=' + record.get('jyPayImgUrl');
                        var form = Ext.create(module + '.view.ViewSellJyForm');
                        var title = Ext.String.format("确认向 -> {0}({1}) 支付 {2}",
                            record.get("alipayRealName"),
                            record.get("alipayNickname"),
                            record.get("jyAmount"),
                        );
                        var win = Ext.create('Fxt.core.view.Window', {
                            height: 600,
                            title: title,
                            items: [form],
                            buttons: [{
                                text: '确认交易',
                                handler: function() {
                                    me.$confirmSellJy(win, record.get('jyId'), function() {
                                        win.close();
                                    });
                                }
                            }]
                        });
                        form.down('image').setSrc(payImgUrl);
                    }
                }
            }
        });
    },
    $confirmSellJy: function(source, jyId, callback) {
        var me = this;
        Fxt.ajax({
            method: 'POST',
            maskCmp: source,
            url: '/stl/confirm-sell-jy',
            params: {
                jyId: jyId
            },
            success: function (data) {
                me.$doQuery();
                if (callback) {
                    callback();
                }
            }
        });
    },
    $newSell: function(alipayAccountId) {
        var me = this;
        Fxt.ajax({
            url: '/stl/new-sell',
            params: {
                alipayAccountId: alipayAccountId
            },
            success: function() {
                me.$doQuery();
            }
        });
    },
    $forceSetSellNewAlipay: function(id, alipayAccountId) {
        var me = this;
        Fxt.ajax({
            url: '/stl/force-set-sell-new-alipay',
            params: {
                id: id,
                alipayAccountId: alipayAccountId
            },
            success: function() {
                me.$doQuery();
            }
        });
    }
});